import axios from 'axios'
import { nanoid } from 'nanoid'
export default {
    namespaced: true,
    actions: {
        addPersonWang(context, value) {
            if (value.name.indexOf('王') === 0) {
                context.commit("addPerson", value);
            } else {
                alert('添加的人必须姓王')
            }
        },
        addPersonServer(context, value) {
            axios.get('https://api.uixsj.cn/hitokoto/get?type=social').then(response => {
                context.commit('addPerson', { id: nanoid(), name: response.data })
            }, error => {
                alert(error.message)
            })
        }
    },
    mutations: {
        addPerson(state, value) {
            state.persons.unshift(value)
        }
    },
    state: {
        persons: [
            { id: 1, name: '张三' }
        ]
    },
    getters: {
        firstPersonName(state) {
            return state.persons[0].name
        }
    }
}